package SvnRep;
use lib "./Config-IniFiles-2.68/lib";
use Config::IniFiles;
use cfg;

my $svn_root=cfg::get('svn_root');
my $svn_bin_path=cfg::get('svn_bin_path');
my $mod_user=cfg::get('mod_user');
my $mod_group=cfg::get('mod_group');

sub get_serve_ini{
	#
	# get_serve_ini( Rep_name )
	#

	$svnserve_conf="$svn_root/$_[0]/conf/svnserve.conf";
	open(FP,$svnserve_conf);
	$svn_cfg_str="";
	#DOS2Unix
	while($tmp=<FP>){
		$tmp=~s/\r\n$/\n/;
		$svn_cfg_str.=$tmp;
	}
	close(FP);
        open(FP,'<',\$svn_cfg_str);
        $rep_cfg=new Config::IniFiles -file => *FP;
	return $rep_cfg;
}

sub get_pwd_ini_path{
	#
	# get_pwd_ini_path( Rep_name )
	#
	$rep_cfg=get_serve_ini($_[0]);
	$pwd_ini=$rep_cfg->val('general','password-db');
	$pwd_ini="$svn_root/$_[0]/conf/$pwd_ini";
	return $pwd_ini;
}

sub get_pwd_ini{
	#
	# get_pwd_ini( Rep_name )
	#
        $pwd_ini=get_pwd_ini_path($_[0]);
        open(FP,$pwd_ini);
        $pwd_ini_str="";
        #DOS2Unix
        while($tmp=<FP>){
                $tmp=~s/\r\n$/\n/;
                $pwd_ini_str.=$tmp;
        }
        close(FP);
        open(FP,'<',\$pwd_ini_str);
	$pwd_ini=null;
        $pwd_ini=new Config::IniFiles -file => *FP;
        return $pwd_ini;
}

sub get_rep_list{
	@rep_list=();
	opendir(DIR,$svn_root) or die "Can't opendir $svn_root";
	while ($svn_dir=readdir(DIR)){
		if(!($svn_dir=~/^\./)&&( -r "$svn_root/$svn_dir/conf/svnserve.conf")){
			push(@rep_list,$svn_dir);
		}
	}
	return @rep_list;
}

sub get_rep_context{
        #
        # get_rep_access( Rep_name )
        #

	$rep_cfg=get_serve_ini($_[0]);
	$cfg_context=$rep_cfg->val("general","realm");
	return $cfg_context;	
}

sub get_rep_access{
	#
	# get_rep_access( Rep_name )
	#

	$rep_cfg=get_serve_ini($_[0]);
	$anon=$rep_cfg->val('general','anon-access');
	$auth=$rep_cfg->val('general','auth-access');
	@rep_acc=($anon,$auth);
	return @rep_acc;
}

sub get_rep_rev{
	#
	# get_rep_rev( Rep_name )
	#

	$rev=`$svn_bin_path/svnlook youngest $svn_root/$_[0]`;
	return $rev;
}

sub get_rep_users{
	#
	# get_rep_users ( Rep_name )
	#
	$pwd_ini=get_pwd_ini($_[0]);
	if($pwd_ini ne ""){
		@users=$pwd_ini->Parameters('users');
	}else{
		#if no any user.
		@users=();
	}
	return @users;
}

sub get_user_in_rep{
	#
	# get_user_in_rep ( User )
	#
	@rep_list=get_rep_list();
	@in_rep=();
	foreach $rep_name (@rep_list){
		@users=get_rep_users($rep_name);
		foreach $user (@users){
			if($user eq $_[0]){
				push(@in_rep,$rep_name);
			}
		}
	}
	return @in_rep;
}

sub set_user_pwd{
	#
	# set_user_pwd( Rep_name ,User ,New_pwd)
	#
	$pwd_ini_path=get_pwd_ini_path($_[0]);
	$pwd_ini=get_pwd_ini($_[0]);
	$pwd_ini->setval('users',$_[1],$_[2]);
	$pwd_ini->WriteConfig($pwd_ini_path);
}

sub set_pwd_allrep{
	#
	# set_pwd_allrep( User, Pwd)
	#
	@rep=get_user_in_rep($_[0]);
	foreach $rep_name (@rep){
		set_user_pwd($rep_name,$_[0],$_[1]);
	}
}

sub create_rep{
	#
	# create_rep( Rep_name )
	#
	`/bin/mkdir $svn_root/$_[0]`;
	$r=`$svn_bin_path/svnadmin create $svn_root/$_[0]`;
	`/bin/chown -R $mod_user.$mod_group $svn_root/$_[0]`;
}

sub set_context{
	#
	# set_context ( Rep_name, Context)
	#
	$rep_ini_path="$svn_root/$_[0]/conf/svnserve.conf";
	$rep_cfg=get_serve_ini($_[0]);
	$r=$rep_cfg->setval("general","realm",$_[1]);
	if($r eq undef){
		$rep_cfg->newval("general","realm",$_[1]);	
	}
	$rep_cfg->WriteConfig($rep_ini_path);
}

sub set_access{
	#
	# set_access (Rep_name, Anon-access, Auth-access)
	#
	$rep_ini_path="$svn_root/$_[0]/conf/svnserve.conf";
	$rep_cfg=get_serve_ini($_[0]);
	$r=$rep_cfg->val("general","password-db");
	if($r eq undef){
		$rep_cfg->newval("general","password-db","passwd");
	}

	#
	# Auth
	#

	$r=$rep_cfg->setval("general","anon-access",$_[1]);
	if($r eq undef){
		$rep_cfg->newval("general","anon-access",$_[1]);
	}
	$r=$rep_cfg->setval("general","auth-access",$_[2]);
	if($r eq undef){
		$rep_cfg->newval("general","auth-access",$_[2]);
	}
	$rep_cfg->WriteConfig($rep_ini_path);
}

sub del_rep{
	#
	# del_rep ( Rep_name )
	#
	`/bin/rm -rf $svn_root/$_[0]`
}

sub del_user_in_rep{
	#
	# del_user_in_rep ( $uid, $rep )
	#
	$uid=$_[0];
	$rep=$_[1];
	$ini=get_pwd_ini_path($rep);
	$cfg=get_pwd_ini($rep);
	$cfg->delval('users',$uid);
	$cfg->WriteConfig($ini);
}

sub del_user_allrep{
	#
	# del_user_allrep( $uid )
	#
	$uid=$_[0];
	@rep=get_user_in_rep($uid);
	foreach $rep_name (@rep){
		del_user_in_rep($uid,$rep_name);
	}
}

sub add_user_to_rep{
	#
	# add_user_to_repo ( $uid, $pwd, $repo)
	#
	$uid=$_[0];
	$pwd=$_[1];
	$repo=$_[2];
	$ini=get_pwd_ini_path($repo);
	$cfg=get_pwd_ini($repo);
	$r=$cfg->exists('users',$uid);
	if($r!=1){
		$cfg->newval('users',$uid,$pwd);
		$cfg->WriteConfig($ini);
	}	
}
